package com.zhaojun.cloud.service.sso.resolver;

import com.zhaojun.cloud.common.exception.AssertionException;
import com.zhaojun.cloud.common.result.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Dave on 2018/6/16.
 */
@ControllerAdvice({"com.zhaojun.cloud.service.sso.controller.*"})
public class GlobalExceptionResolver {

    Logger logger = LoggerFactory.getLogger(GlobalExceptionResolver.class);


    @ExceptionHandler(value = AssertionException.class)
    @ResponseBody
    public CommonResult AessertionExceptionHandler(HttpServletRequest request, Exception e) {
        // 对捕获的自定义异常进行处理并打印日志等，之后返回json数据，方式与Controller相同
        logger.error(e.getMessage(), e);
        return CommonResult.error(e.getMessage());
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult exceptionHandler(HttpServletRequest request, Exception e) {
        // 对捕获的异常进行处理并打印日志等，之后返回json数据，方式与Controller相同
        logger.error(e.getMessage(), e);
        return CommonResult.error("系统错误！");
    }
}